#include <stdlib.h>
#include "knuth_stack.h"



struct dstack{
	element_t data[SIZE];
	int left_edge;	/*this edge shown where WILL the next element go*/
	int right_edge; /*this edge shown where WILL the next element go*/
	};

stack create(void)
{stack ptr;
 if( (ptr=malloc(sizeof(struct dstack)) ) == NULL )
	return NULL;
 ptr->left_edge=0;		/*initialize the left edge*/
 ptr->right_edge=SIZE-1;        /*initialize the right edge*/
 return ptr;
}

bool empty(stack target_stack,sel stack_selection)
{if(target_stack->left_edge == 0 && stack_selection == left_stack)	   /*I check the left edge if is 0 */
	return true;
 if(target_stack->right_edge == SIZE-1 && stack_selection == right_stack)  /*I check if the left edge is SIZE-1*/
	return true;
 return false;
}

bool full(stack target_stack)
{if( target_stack->left_edge > target_stack->right_edge ) /*if the left edge has overcome the right*/
	return true;					  /*the stack is full*/
 return false;
}

bool pop(stack target_stack,element_t *data,sel stack_selection)
{if(stack_selection == left_stack){
	if(empty(target_stack,left_stack) == true)
		return false;
	target_stack->left_edge--;					/*change the edge*/
	*data = target_stack -> data[target_stack -> left_edge];	/*save the data*/
	}
else{
	if(empty(target_stack,right_stack) == true)
		return false;
	target_stack->right_edge++;					/*change the edge*/
	*data=target_stack->data[target_stack->right_edge];		/*save the data*/
	}
return true;
}

bool push(stack target_stack,element_t data,sel stack_selection)
{if( full(target_stack) == true)
	return false;
 else{
	if(stack_selection == left_stack){
		target_stack -> data[ target_stack -> left_edge ]= data;	/*store the data*/
		target_stack -> left_edge++;					/*change the edge*/
		}
	else{
		target_stack -> data[ target_stack -> right_edge] = data;	/*store the data*/
		target_stack -> right_edge--;					/*change the edge*/
		}
	return true;
	}
}

bool top(stack target_stack,sel stack_selection,element_t *data)
{if(stack_selection == left_stack){
	if(empty(target_stack,left_stack) == true)
		return false;
	*data = target_stack -> data[target_stack -> left_edge-1];	/*take the data whithout changing the edge*/
	}	
else {
	if(empty(target_stack,right_stack) ==true )
		return false;
	*data = target_stack -> data[target_stack->right_edge+1];	/*take the data whithout changin the edge*/
	}
return true;
}

